package com.yr.ryjk.pay;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unionpay.acp.UnionPayBackRcvResponseHandler;
import com.yr.ryjk.common.Constant;
import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.pay.service.IPayService;
import com.yr.ryjk.util.JSONUtil;
/**
 * 支付和银行相关
 * @author Rick
 *
 */
@Controller
@RequestMapping("/pay")
@ResponseBody
public class PayController {
	@Autowired
	private IPayService payService;
	
	/**
	 * 医生添加个人银行卡
	 * @param dId 区域id
	 * @param openBank 开户行
	 * @param cardNo 银行卡号
	 * @param token 令牌
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/addCard",method=RequestMethod.POST)
	public Map<String, Object> addCard(@RequestParam("dId") @NotNull Integer dId,
			@RequestParam("openBank") @NotBlank String openBank,
			@RequestParam("cardNo") @NotBlank String cardNo,
			@RequestParam("token") @NotBlank String token){
		return payService.addCardToDoctor(dId,openBank,cardNo,token);
	}
	
	/**
	 * 医生删除个人银行卡
	 * @param cardId 银行卡id
	 * @param token 用于验证身份
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/delCard",method=RequestMethod.POST)
	public Map<String, Object> delCard(@RequestParam("cardId") @NotBlank String cardId,
			@RequestParam("token") @NotBlank String token){
		payService.deleteCard(cardId,token);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 医生获取个人所有银行卡信息
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/allCard",method=RequestMethod.GET)
	public Map<String, Object> getAllCardOfDoc(@RequestParam("token") @NotBlank String token){
		return JSONUtil.getJSON(ErrorType.SUCCESS,payService.getAllCardOfDoc(token));
	}
	
	/**
	 * 医生请求提现,冻结这部分的健康币
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/cashOut",method=RequestMethod.POST)
	public Map<String, Object> cashOut(@RequestParam("cardId") @NotBlank String cardId,
			@RequestParam("coinAmount") @Min(0) int coinAmount,
			@RequestParam("token") @NotBlank String token){
		return payService.addCashOut(cardId,coinAmount,token);
	}
	
	/**
	 * app分页获取自己的提现申请记录
	 * @param pageNo
	 * @param pageSize
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/listCashOut",method=RequestMethod.GET)
	public Map<String, Object> cashOut(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="count",defaultValue="10") int pageSize,
			@RequestParam("token") @NotBlank String token){
		return payService.listCashOut(pageNo,pageSize,token);
	}
	
	/**
	 * app提交充值订单接口
	 * @param type 充值类型,0为支付宝,1为银联
	 * @param amount 金额,分为单位
	 * @param token 令牌,确定是谁充值
	 * @return
	 * @throws ParseException 
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/submitOrder",method=RequestMethod.POST)
	public Map<String, Object> submitOrder(@RequestParam("type") int type,
			@RequestParam("amount") @Min(10) int amount,
			@RequestParam("token") @NotBlank String token) throws ParseException{
		return payService.submitOrder(type,amount,token);
	}
	
	/**
	 * 接收银联异步应答,不拦截
	 * @param type
	 * @param amount
	 * @param token
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @throws ParseException
	 */
	@RequestMapping(value="/acceptResult",method=RequestMethod.POST)
	public Map<String, Object> acceptResult(HttpServletRequest request) throws UnsupportedEncodingException, ParseException{
		UnionPayBackRcvResponseHandler.handleRequest(request, payService);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 后台提现申请记录
	 * @param pageNo
	 * @param pageSize
	 * @param status 过滤条件,处理状态,0为未处理,1为已处理
	 * @param name 过滤条件,医生姓名
	 * @param phone 过滤条件,医生手机号
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashout/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listCashOut(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
			@RequestParam(value="status", defaultValue = "0") int status,
			String name,String phone) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,
				payService.getListOfPage(pageNo, pageSize, status,name,phone));
	}
	
	/**
	 * 后台批量更新提现申请记录
	 * @param ids id集合，逗号分隔
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashout/solve", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> solveCashOut(@RequestParam("ids") @NotBlank String ids) {
		payService.updateSolveCashout(ids);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 查看详情
	 * @param id
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashout/get", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCashOut(@RequestParam("id") @NotBlank String id) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,payService.getByPrimaryKey(id));
	}
	
	/**
	 * 后台提现申请记录数据导出
	 * @param pageNo
	 * @param pageSize
	 * @param status
	 * @param name
	 * @param phone
	 * @return
	 * @throws Exception 
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashout/excel", method = RequestMethod.GET)
	public void exportExcel(@RequestParam(value="status", defaultValue = "0") int status,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="+ System.currentTimeMillis()+ ".xls");
		OutputStream os=response.getOutputStream();
		payService.exportExcel(status,os);
	}	
	/**
	 * app获取提现金额到期时间提示信息
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/getCashOutTip",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCashOutTip(){
		return JSONUtil.getJSON(ErrorType.SUCCESS, payService.getCashOutTip());
	}
	
	/**
	 * 后台充值记录
	 * @param pageNo
	 * @param pageSize
	 * @param operate_time 过滤条件,充值时间
	 * @param name 过滤条件,医生姓名
	 * @param phone 过滤条件,医生手机号
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashIn/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> cashInRecord(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
			@RequestParam("inTime") @DateTimeFormat(pattern=Constant.DATE_FORMAT) Date inTime,
			String name,String phone) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,				
				payService.getListOfCashInPage(pageNo, pageSize, inTime,name,phone));
	}

	/**
	 * 查看充值记录详情
	 * @param id
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashIn/getInfo", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getRecharge(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,@RequestParam("userId") @NotBlank String userId) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,payService.getCashInRecordByPrimaryKey(pageNo, pageSize,userId));
	}
	
	/**
	 * 后台充值记录数据导出
	 * @param pageNo
	 * @param pageSize
	 * @param status
	 * @param name
	 * @param phone
	 * @return
	 * @throws Exception 
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/cashIn/excel", method = RequestMethod.GET)
	public void exportCoinRechargeExcel(@RequestParam(value="userId") String userId,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="+ System.currentTimeMillis()+ ".xls");
		OutputStream os=response.getOutputStream();
		payService.exportCashInExcel(userId,os);
	}
}